<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/4/2
 * Time: 11:24
 */

namespace app\index\logic;

use app\common\model\Property;
use app\common\utils\Constant;
use Exception;
use think\Db;
use think\Log;
use app\admin\logic\MoneyLogic;
class PayLogic
{
    /**
     * @param $order_number string 订单编号
     * @param $money float 总金额
     * @param  $type 0微信支付 1支付宝支付 2银行卡支付
     * @return int 0余额不足 1支付成功 2支付失败
     */
    public function weChatPaySuccess($order_number,$money,$type)
    {
        $where['order_sn']= $order_number;
        $order = model('Order')->where($where)->find();
        if($order){
//            if($recharge_order['state'] == 0 && is_numeric($money) && $recharge_order['money'] <= $money) {
            if($order['status'] == 0 && is_numeric($money)) {
                return $this->paySuccess($order);//支付成功
            }
        }
        return 2;
    }

    /**
     * 创建缴费订单
     * @param $house_property_id int 房产id
     * @param $property_charge float 物业费
     * @param $water_charge float 水费
     * @param $total_money float 总金额
     * @param $user_id int 用户id
     * @return mixed
     */
    public function doPay($house_property_id,$property_charge,$water_charge,$total_money,$user_id){
        Db::startTrans();
        try {
            $result = $this->checkData($house_property_id, $property_charge, $water_charge, $total_money, $user_id);
            if ($result['status'] != 1) {
                return $result;
            }
            $house_property = $result['data'];
            $property_id = model('Village')->where(array('id'=>$house_property['village_id']))->value('property_id');
            //创建订单信息
            $order['order_sn'] = build_order_no();
            $order['status'] = 0;
            $order['pay_type'] = 0;
            $order['house_property_id'] = $house_property_id;
            $order['village_id'] = $house_property['village_id'];
            $order['property_id'] = $property_id;
            $order['money'] = $total_money;
            $order['property_charge'] = $property_charge;
            $order['water_charge'] = $water_charge;
            $order['user_id'] = $user_id;
            $order['create_time'] = time();
            model('Order')->insert($order);
            $data['openid'] = model('User')->where(array('id'=>$user_id))->value('openid');
            $data['order_sn'] = $order['order_sn'];
            $data['money'] = $order['money'];
            Db::commit();
            return array('status'=>1,'data'=>$data);
        }catch (Exception $e){
            Db::rollback();
            return array('status'=>0,'error'=>$e->getMessage());
        }
    }

    private function checkData($house_property_id,$property_charge,$water_charge,$total_money,$user_id){
        if($property_charge == 0 && $water_charge == 0){
            return array('status'=>2,'msg'=>'请选择费用');
        }
        if(!is_numeric($property_charge) || $property_charge < 0){
            return array('status'=>2,'msg'=>'物业费非法');
        }
        if(!is_numeric($water_charge) || $water_charge < 0){
            return array('status'=>2,'msg'=>'水费非法');
        }
        if(!is_numeric($total_money) || $total_money < 0){
            return array('status'=>2,'msg'=>'总计费用非法');
        }
        if(($property_charge + $water_charge) != $total_money){
            return array('status'=>2,'msg'=>'总计费用非法');
        }
        if($total_money <= 0){
            return array('status'=>2,'msg'=>'无需缴费');
        }
        $house_property = model('HouseProperty')->where(array('id'=>$house_property_id))->find();
        if(!$house_property){
            return array('status'=>2,'msg'=>'房产记录不存在');
        }
        if($house_property['user_id'] !=$user_id){
            return array('status'=>2,'msg'=>'只有房产所属者才能缴费');
        }
        if($property_charge > $house_property['property_charge']){
            return array('status'=>2,'msg'=>'物业费有误');
        }
        if($water_charge > $house_property['water_charge']){
            return array('status'=>2,'msg'=>'水费费有误');
        }
        return array('status'=>1,'data'=>$house_property);
    }

    /**
     * 支付成功判断
     * @param $order
     * @return bool|int
     */
    public function paySuccess($order){
        Db::startTrans();
        try {
            //更新房产信息
            $map['id'] = $order['house_property_id'];
            $house_property = model('HouseProperty')->where($map)->find();
            $update['property_charge'] = $house_property['property_charge'] - $order['property_charge'];
            $update['water_charge'] = $house_property['water_charge'] - $order['water_charge'];
            $update['charge_total'] = $update['water_charge'] + $update['property_charge'];
            model('HouseProperty')->where($map)->update($update);
            //更新物业的可用余额
            $propertyId = model("Village")->where("id={$house_property['village_id']}")->value("property_id");
            $property = model('Property')->where("id={$propertyId}")->find();
            model('Property')->where("id={$propertyId}")->setInc('money_over',$order['property_charge']+$order['water_charge']);
            //添加记录到资金变动表
            $this->addMoneyLog($house_property, $property);
            //更新订单信息
            $update_order['pay_time'] = time();
            $update_order['status'] = 1;
            model('Order')->where(array('id'=>$order['id']))->update($update_order);
            Db::commit();
            return 1;
        }catch (Exception $e){
            Db::rollback();
            Log::write('paySuccess:'.$e->getMessage());
            return false;
        }
    }

    /**
     * 添加记录
     * @param $house_property
     * @param $property
     */
    private function addMoneyLog($house_property, $property){
        //记录水费
        MoneyLogic::getInstance()->saveMoneyLog($house_property['water_charge'], $property['money_over']+$house_property['water_charge']
            ,Constant::CHANGE_TYPE_USER_PAY, Constant::IN_OUT_TYPE_IN,'用户缴水费收入'
            ,$house_property['user_id'], Constant::MONEY_TYPE_WATER, $property['id'], $house_property['id']
        );
        //记录物业费
        MoneyLogic::getInstance()->saveMoneyLog($house_property['property_charge'], $property['money_over']+$house_property['property_charge']
            ,Constant::CHANGE_TYPE_USER_PAY, Constant::IN_OUT_TYPE_IN,'用户缴物业费收入'
            ,$house_property['user_id'], Constant::MONEY_TYPE_PROPERTY, $property['id'], $house_property['id']
        );
    }
}